<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2020/4/8
 * Time: 16:07
 */

namespace App\Model;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{

    protected $table  =  'it_user';
    protected $primaryKey = 'id';
    protected $fillable = ['name','password'];
    public    $timestamps = false;
    protected $connection = 'mysql_city';


    /**
     * 一对一关联
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function UserInfo(){
        return $this->hasOne('App\Model\UserInfo','user_id');//注:hasOne不可设置localKey，否则无法获取数据
    }

    /**
     * 一对多关联
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function UserLogs()
    {
        return $this->hasMany('App\Model\UserLogs','user_id','id');//1.related -> 关联的模型，2.foreignKey->关联的字段，3.localKey->关联表的主键
    }

    /**
     * 一对多关联
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function Article()
    {
        return $this->hasMany('App\Model\Article','user_id','id');//1.related -> 关联的模型，2.foreignKey->关联的字段，3.localKey->关联表的主键
    }

    /**
     * 多对一关联
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function Country()
    {
        return $this->belongsTo('App\Model\Country','country_id');
    }

    /**
     * 多对多关联
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function Role(){
        /*
         * 第一个参数：要关联的表对应的类
         * 第二个参数：中间表的表名
         * 第三个参数：当前表跟中间表对应的外键
         * 第四个参数：要关联的表跟中间表对应的外键
         * */
        return $this->belongsToMany('App\Model\Role','it_user_role','user_id','role_id');
    }



}
